using System;
using UnityDemo.DataSource.NorthwindService;
using UnityDemo.LoggingUtility;

namespace UnityDemo.DemoCode
{

    internal class ServiceProvider1 : IServiceProvider
    {
        public void GetEmployeeNames()
        {
         
        }

        public string Name
        {
            get { return " Service prov One"; }
            set
            {
                
            }
        }

        public override string ToString()
        {
            return "My Current Name is " + Name;
        }
    }


    internal class ServiceProvider : IServiceProvider
    {
        private readonly NorthwindEntities _northwindEntities;

        private NorthwindEntities NorthwindContext
        {
            get
            {
                return _northwindEntities;
            }
        }

        public string Name
        {
            get; 
            set;
        }

        public override string ToString()
        {
            return "My Current Name is "+ Name;
        }

        public ServiceProvider()
        {
            LogerInstance.Logger.Initialize(this);
          //  LogerInstance.Logger.StartWatch("Started an Expensive process will take time . Plz wait");
            try
            {
                if (_northwindEntities == null)
                {
                    if (DataSource.NorthwindServieInstance.Context == null)
                        DataSource.NorthwindServieInstance.Initialize();
                    _northwindEntities = DataSource.NorthwindServieInstance.Context;
                }

            //    LogerInstance.Logger.StopWatch();

                Name = "My current Name is Service provider";
            }
            catch (Exception ex)
            {
                LogerInstance.Logger.Exception(ex);
            }

            //simulate expensive initialization
        }


       

        public ServiceProvider(Uri uri)
        {
            LogerInstance.Logger.Initialize(this);

            try
            {
                if (uri == null)
                {
                    if (DataSource.NorthwindServieInstance.Context == null)
                        DataSource.NorthwindServieInstance.Initialize();
                    _northwindEntities = DataSource.NorthwindServieInstance.Context;
                }
                else
                {
                    _northwindEntities = new NorthwindEntities(uri);
                }

                //    LogerInstance.Logger.StopWatch();

                Name = "My Name has been updated with uri ";
            }
            catch (Exception ex)
            {
                LogerInstance.Logger.Exception(ex);
            }

            //simulate expensive initialization
        }


        public void GetEmployeeNames()
        {
            var employees = NorthwindContext.Employees;

            LogerInstance.Logger.StartWatch(" Displaying list of Employees. Fetching data from service. Please wait...");
            bool _fetchComplete = false;
            foreach (var employe in employees)
            {
                if (!_fetchComplete)
                {
                    LogerInstance.Logger.StopWatch();
                    _fetchComplete = true;
                }

                LogerInstance.Logger.Info(employe.FirstName + " " + employe.LastName);
            }
        }
    }
}